package cn.usian.service.employeeserviceimpl;

import cn.usian.common.R;
import cn.usian.dao.SetmealDishMapper;
import cn.usian.dao.SetmealMapper;
import cn.usian.entity.Setmeal;
import cn.usian.entity.SetmealDTO;
import cn.usian.entity.SetmealDish;
import cn.usian.service.SetmealService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDTO> setmealPage = new Page<>(page,pageSize);
        List<SetmealDTO>setmealList=setmealMapper.findPage(setmealPage,name);
        setmealPage.setRecords(setmealList);
        return R.success(setmealPage);
    }

    @Override
    public R delSetmeal(Long[] ids) {
            for (Long id : ids) {
                setmealMapper.updateIsById(id);
            }
        return R.success("删除成功");
    }

    @Override
    public void updateone(Long[] ids, Integer status) {
        for (Long id : ids) {
            setmealMapper.updateone(id,status);
        }
    }

    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO,Long id) {
            setmealDTO.setCreateTime(LocalDateTime.now());
            setmealDTO.setUpdateTime(LocalDateTime.now());
            setmealDTO.setStatus(1);
            setmealDTO.setCode(null);
            setmealMapper.insert(setmealDTO);

        SetmealDish setmealDish = new SetmealDish();
        setmealDish.setSetmealId(setmealDTO.getId());
        setmealDish.setCopies(1);
        setmealDish.setCreateTime(LocalDateTime.now());
        setmealDish.setUpdateTime(LocalDateTime.now());
        setmealDish.setCreateUser(id);
        setmealDish.setUpdateUser(id);


    }
}
